

import Foundation

protocol DIContainerProtocol {
  func register<Component>(type: Component.Type, component: Any)
  func resolve<Component>(type: Component.Type) -> Component?
}

final class DIContainer: DIContainerProtocol {
  // 采用单例模式
  static let shared = DIContainer()
  
  // 禁止外界使用init初始化
  private init() {}

  // 用字典保存依赖对象
  var components: [String: Any] = [:]

  func register<Component>(type: Component.Type, component: Any) {
    // 注册
    components["\(type)"] = component
  }

  func resolve<Component>(type: Component.Type) -> Component? {
    // 取出准备注入
    return components["\(type)"] as? Component
  }
}
